package wang.unclecat.mycard.slice;

import ohos.aafwk.ability.IAbilityConnection;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.utils.TextTool;
import ohos.agp.window.dialog.ToastDialog;
import ohos.bundle.ElementName;
import ohos.rpc.IRemoteObject;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.rpc.RemoteException;
import ohos.utils.net.Uri;
import wang.unclecat.mycard.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import wang.unclecat.mycard.ServiceAbility;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class CardAbilitySlice extends AbilitySlice {
    private IRemoteObject remote;

    // 创建连接Service回调实例
    private IAbilityConnection connection = new IAbilityConnection() {
        // 连接到Service的回调
        @Override
        public void onAbilityConnectDone(ElementName elementName, IRemoteObject iRemoteObject, int resultCode) {
            // Client侧需要定义与Service侧相同的IRemoteObject实现类。开发者获取服务端传过来IRemoteObject对象，并从中解析出服务端传过来的信息。
            remote = iRemoteObject;
        }

        // Service异常死亡的回调
        @Override
        public void onAbilityDisconnectDone(ElementName elementName, int resultCode) {
            remote = null;
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_card);
        String msg = intent.getStringParam("msg");
        if (!TextTool.isNullOrEmpty(msg)) {
            new ToastDialog(getContext()).setText(msg).show();
        }

        findComponentById(ResourceTable.Id_text_go2Apk).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                Intent intent = new Intent();
                Set<String> entities = new HashSet<>();
                entities.add("entity.user.c1");

                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("wang.unclecat.hello")//your android app’s packagename
                        .withAbilityName("wang.unclecat.hello.MainActivity")//your android app’s activity fullname
                        .withFlags(Intent.FLAG_NOT_OHOS_COMPONENT)
                        .withUri(Uri.parse("https://www.baidu.com"))
                        .withEntities(entities)
                        .build();
                intent.setOperation(operation);

                IntentParams params = new IntentParams();
                params.setParam("androidFile1","androidFile1----");
                intent.setParams(params);
                startAbility(intent);
                terminate();//我们认为MainActivity是应用的首页，所以要把当前Slice终止掉，这样比较符合使用习惯。
            }
        });
        findComponentById(ResourceTable.Id_text_service).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
//                // 启动ServiceAbility
//                Intent intentService = new Intent();
//                Operation operation =
//                        new Intent.OperationBuilder()
//                                .withDeviceId("")
//                                .withBundleName(getBundleName())
//                                .withAbilityName(ServiceAbility.class.getName())
//                                .build();
//                intentService.setOperation(operation);
//                startAbility(intentService);

                // 连接Service
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName(getBundleName())
                        .withAbilityName(ServiceAbility.class.getName())
                        .build();
                intent.setOperation(operation);
                connectAbility(intent, connection);
            }
        });

        findComponentById(ResourceTable.Id_send).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (remote!=null) {
                    MessageParcel data = MessageParcel.obtain();
                    MessageParcel reply = MessageParcel.obtain();
                    MessageOption option = new MessageOption(MessageOption.TF_SYNC);

                    data.writeString("time:"+new Date().getTime());

                    try {
                        remote.sendRequest(1, data, reply, option);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
